#!/usr/bin/env python

import sys, getopt

import utility


def uncompress_files_in_range(begin_index, end_index):
    job_events_prefix = "job_events/"
    task_usage_prefix = "task_usage/"
    file_name_prefix = "part-00"
    file_name_suffix = "-of-00500.csv"

    for i in range(begin_index, end_index):
        if i < 10:
            file_name = file_name_prefix+"00"+str(i)+file_name_suffix
        elif i < 100:
            file_name = file_name_prefix+"0"+str(i)+file_name_suffix
        else:
            file_name = file_name_prefix+str(i)+file_name_suffix
        
        utility.uncompressed_file(job_events_prefix+file_name)
        utility.uncompressed_file(task_usage_prefix+file_name)


def main(argv):
    try:
        opts, args = getopt.getopt(argv[1:], "hb:e:")
    except getopt.GetoptError as err:
        print(err)
        sys.exit(2)

    # set default value
    begin_index = 0
    end_index = 500

    for o, s in opts:
        # help information
        if o == "-h":
            print "usage  -b <begin package index>"
            print "       -e <end package index>"
            return
        elif o == "-b":
            begin_index = int(s)
        elif o == "-e":
            end_index = int(s)
    
    uncompress_files_in_range(begin_index, end_index)


if __name__ == "__main__":
    main(sys.argv)